package com.ruoyi.business.controller;


import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author liumaotong
 * @Description 共通接口
 * @Date 2024/6/17
 */
@RestController
@RequestMapping("/mobile/commonApi")
public class CommonApiController {

    @Value("${file.path}")
    private String filePath;
    @Value("${file.serverUrl}")
    private String url;

    @RequestMapping("/uploadFile")
    public AjaxResult uploadFile(@RequestParam("file") MultipartFile file) throws IOException {

        /*获取文件原名称*/
        String originalFilename = file.getOriginalFilename();
        /*获取文件格式*/
        String fileFormat = originalFilename.substring(originalFilename.lastIndexOf("."));

        String fileName = IdUtils.fastSimpleUUID() + fileFormat;
        file.transferTo(new File(filePath + fileName));

        Map<String,String> res = new HashMap<>(1);
        res.put("url",url + fileName);
        return AjaxResult.success(res);
    }

}
